
<%@ page import="ratemymacclass.Course" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="main" />
        <g:set var="entityName" value="${message(code: 'course.label', default: 'Course')}" />
        <title>${courseInstance?.title}<g:message args="[entityName]" /></title>
    </head>
    <body>
        <div class="nav" style="text-align:center">
            <span class="menuButton"><a class="home" href="${createLink(uri: '/')}"><g:message code="Home"/></a></span>
            <span class="menuButton"><g:link class="list" action="list">List of Courses</g:link></span>
            <span class="menuButton"><g:link controller="professor" class="list" action="list">List of Professors</g:link></span>

        </div>
        <div id="header" style="padding-bottom: 0px">
            <br/><h1>${courseInstance?.department} ${courseInstance?.courseNum}: ${courseInstance?.title}</h1><br />
            <g:def var="ratedCourse" value="${courseInstance?.id}"/>
                <button><g:link controller="rating" class="create" action="create" params="[courseID: ratedCourse]">Rate this course</g:link></button><br />
        </div>
        <g:if test="${!(courseInstance.ratings)}">
            This course has no ratings yet.
            %{--<g:link controller="rating" class="create" action="create">Rate this course now!</g:link>--}%
        </g:if>
        <g:else>
        <div id="big rating div">
            <div class="averages"><br />
                <h2 style="text-align:center">Average Ratings for ${courseInstance?.title}</h2><br />
                <table style="width: 50%; margin-left:auto; margin-right:auto; text-align: center;" border="0">
                <tr>
                    <td style="text-align:center"><g:message code="ratings.workload.label" default="Workload" /></td>
                    <td style="text-align:center"><g:message code="ratings.easiness.label" default="Easiness" /></td>
                    <td style="text-align:center"><g:message code="ratings.useful.label" default="Useful" /></td>
                    <td style="text-align:center"><g:message code="ratings.learning.label" default="Learning" /></td>
                </tr>

                <tr>
                    <td style="text-align:center">${(int )courseInstance?.ratings?.workload?.sum()/courseInstance?.ratings?.workload?.size()}</td>
                    <td style="text-align:center">${(int)courseInstance?.ratings?.easiness?.sum()/courseInstance?.ratings?.easiness?.size()}</td>
                    <td style="text-align:center">${(int)courseInstance?.ratings?.useful?.sum()/courseInstance?.ratings?.useful?.size()}</td>
                    <td style="text-align:center">${(int)courseInstance?.ratings?.learning?.sum()/courseInstance?.ratings?.learning?.size()}</td>
                </tr>
                <tr><td colspan="4" style="text-align:center; font-weight:bold">
                    ${courseInstance?.ratings?.recommend?.count(true)} raters out of ${courseInstance?.ratings?.recommend?.size()} recommend this course!
                </td></tr>
                </table><br/><br/> <br/>
            </div>
            <div class="list">
                <table style="width:90%; margin-left:auto; margin-right:auto; text-align: center;">
                    <thead>
                        <tr>

                            <%--g:sortableColumn property="id" title="${message(code: 'course.id.label', default: 'Id')}" /--%>

                            <g:sortableColumn property="title" title="Rating Title" />
                            <g:sortableColumn property="prof" title="With Professor:" />
                            <g:sortableColumn property="workload" title="Workload" />
                            <g:sortableColumn property="easiness" title="Easiness" />
                            <g:sortableColumn property="useful" title="Useful" />
                            <g:sortableColumn property="learning" title="Learning" />
                            <g:sortableColumn property="comment" title="Comment" />
                            <g:sortableColumn property="recommend" title="Recommended?" />


                        </tr>
                    </thead>
                    <tbody>
                    <g:each in="${courseInstance.ratings}" var="r">
                        <tr>

                            <%--td><g:link action="show" id="${courseInstance.id}">${fieldValue(bean: courseInstance, field: "id")}</g:link></td--%>

                            <td><g:link controller="rating" action="show" id="${r.id}">${r?.title?.encodeAsHTML()}</g:link></td>
                            <td><g:link controller="professor" action="show" id="${r.id}">${r?.professor?.firstName?.encodeAsHTML()} ${r?.professor?.lastName?.encodeAsHTML()}</g:link></td>
                            <td>${r?.workload?.encodeAsHTML()}</td>
                            <td>${r?.easiness?.encodeAsHTML()}</td>
                            <td>${r?.useful?.encodeAsHTML()}</td>
                            <td>${r?.learning?.encodeAsHTML()}</td>
                            <td>${r?.comment?.encodeAsHTML()}</td>
                            <td>${r?.recommend?.encodeAsHTML()}</td>

                        </tr>
                    </g:each>

                    </tbody>
                </table>
            </div>
        </div>
        </g:else>
            <div class="buttons">
                <g:form>
                    <g:hiddenField name="id" value="${courseInstance?.id}" />
                    <span class="button"><g:actionSubmit class="edit" action="edit" value="${message(code: 'default.button.edit.label', default: 'Edit')}" /></span>
                    <span class="button"><g:actionSubmit class="delete" action="delete" value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" /></span>
                </g:form>
            </div>
        </div>
    </body>
</html>
